//
//  ThirdViewController.m
//  A段
//
//  Created by 月月 on 15/4/10.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ThirdViewController.h"
#import "MyConnection.h"
#import "PinpaiCollectionViewCell.h"
#import "HeadCollectionReusableView.h"
#import "UIImageView+WebCache.h"
#import "PinViewController.h"
#import "BaikeViewController.h"



#define PINPAI @"http://watch-cdn.idailywatch.com/api/list/brands/zh-hans"

@interface ThirdViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
{
    UICollectionView *_collection;
}

@property(nonatomic, retain) NSMutableArray *collectionArray ;
@property(nonatomic, retain) NSMutableArray *arrayAll ;
@property(nonatomic, retain)UIScrollView *view;

@end

@implementation ThirdViewController

- (void)dealloc
{
    [_arrayAll release];
    [_collectionArray release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.collectionArray = [NSMutableArray array];
        self.arrayAll = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIImageView *image = [[UIImageView alloc] init];
    image.frame = self.view.bounds;
    [self.view addSubview: image];
    [image setImage:[UIImage imageNamed:@"图.jpg"]];
    

    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self createImage];
    
    [self createCollection];
    
    [self getData];
}

-(void)createImage
{
    UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(20, 64, self.view.frame.size.width - 40, 50)] autorelease];
    [self.view addSubview:button];
    
    UIImageView *ima = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)] autorelease];
    [ima setImage:[UIImage imageNamed:@"书.png"]];
    [button addSubview:ima];
    
    UILabel *baike = [[[UILabel alloc] initWithFrame:CGRectMake(100, 0, button.frame.size.width - 150, 50)] autorelease];
    [baike setText:@"腕表百科"];
    [button addSubview:baike];
    
    UIImageView *vi = [[[UIImageView alloc] initWithFrame:CGRectMake(button.frame.size.width - 50, 10, 30, 30)] autorelease];
    [vi setImage:[UIImage imageNamed:@"尖.png"]];
    [button addSubview:vi];
    
    
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
}


-(void)buttonAction:(id)sender
{
    BaikeViewController *bai = [[[BaikeViewController alloc] init] autorelease];
    [self.navigationController pushViewController:bai animated:YES];
}



#pragma mark -
#pragma mark 网络请求

-(void)getData
{
   
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"1" forKey:@"page"];
    [dic setObject:@"iphone" forKey:@"ver"];
    [dic setObject:@"9" forKey:@"app_ver"];
    __block ThirdViewController *my = self;
    [MyConnection connectWithUrl:PINPAI parmaters:dic finishBlock:^(NSData *data) {
        my.collectionArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        [my getDATA];
        [_collection reloadData];
    }];
    
}


-(void)getDATA
{
    NSMutableArray *arrayA = [NSMutableArray array];
    NSMutableArray *arrayB = [NSMutableArray array];
    NSMutableArray *arrayC = [NSMutableArray array];
    NSMutableArray *arrayD = [NSMutableArray array];
    NSMutableArray *arrayE = [NSMutableArray array];
    NSMutableArray *arrayF = [NSMutableArray array];
    NSMutableArray *arrayG = [NSMutableArray array];
    NSMutableArray *arrayH = [NSMutableArray array];
    NSMutableArray *arrayI = [NSMutableArray array];
    NSMutableArray *arrayJ = [NSMutableArray array];
//    NSMutableArray *arrayK = [NSMutableArray array];
    NSMutableArray *arrayL = [NSMutableArray array];
    NSMutableArray *arrayM = [NSMutableArray array];
    NSMutableArray *arrayN = [NSMutableArray array];
    NSMutableArray *arrayO = [NSMutableArray array];
    NSMutableArray *arrayP = [NSMutableArray array];
//    NSMutableArray *arrayQ = [NSMutableArray array];
    NSMutableArray *arrayR = [NSMutableArray array];
    NSMutableArray *arrayS = [NSMutableArray array];
    NSMutableArray *arrayT = [NSMutableArray array];
    NSMutableArray *arrayU = [NSMutableArray array];
    NSMutableArray *arrayV = [NSMutableArray array];
//    NSMutableArray *arrayW = [NSMutableArray array];
//    NSMutableArray *arrayX = [NSMutableArray array];
//    NSMutableArray *arrayY = [NSMutableArray array];
    NSMutableArray *arrayZ = [NSMutableArray array];
    for (NSDictionary *dic in self.collectionArray) {
        if ([[dic objectForKey:@"title"] hasPrefix:@"A"]) {
            [arrayA addObject:dic];
        }if ([[dic objectForKey:@"title"] hasPrefix:@"B"]) {
            [arrayB addObject:dic];
        }if ([[dic objectForKey:@"title"] hasPrefix:@"C"]) {
            [arrayC addObject:dic];
        }if ([[dic objectForKey:@"title"] hasPrefix:@"D"]) {
            [arrayD addObject:dic];
        }if ([[dic objectForKey:@"title"] hasPrefix:@"E"]) {
            [arrayE addObject:dic];
        }if ([[dic objectForKey:@"title"] hasPrefix:@"F"]) {
            [arrayF addObject:dic];
        }if ([[dic objectForKey:@"title"] hasPrefix:@"G"]) {
            [arrayG addObject:dic];
        }if ([[dic objectForKey:@"title"] hasPrefix:@"H"]) {
            [arrayH addObject:dic];
        }if ([[dic objectForKey:@"title"] hasPrefix:@"I"]) {
            [arrayI addObject:dic];
        }if ([[dic objectForKey:@"title"] hasPrefix:@"J"]) {
            [arrayJ addObject:dic];
//        }if ([[dic objectForKey:@"title"] hasPrefix:@"K"]) {
//            [arrayK addObject:dic];
        }if ([[dic objectForKey:@"title"] hasPrefix:@"L"]) {
            [arrayL addObject:dic];
        }if ([[dic objectForKey:@"title"] hasPrefix:@"M"]) {
            [arrayM addObject:dic];
        }if ([[dic objectForKey:@"title"] hasPrefix:@"N"]) {
            [arrayN addObject:dic];
        }if ([[dic objectForKey:@"title"] hasPrefix:@"O"]) {
            [arrayO addObject:dic];
        }if ([[dic objectForKey:@"title"] hasPrefix:@"P"]) {
            [arrayP addObject:dic];
//        }if ([[dic objectForKey:@"title"] hasPrefix:@"Q"]) {
//            [arrayQ addObject:dic];
        }if ([[dic objectForKey:@"title"] hasPrefix:@"R"]) {
            [arrayR addObject:dic];
        }if ([[dic objectForKey:@"title"] hasPrefix:@"S"]) {
            [arrayS addObject:dic];
        }if ([[dic objectForKey:@"title"] hasPrefix:@"T"]) {
            [arrayT addObject:dic];
        }if ([[dic objectForKey:@"title"] hasPrefix:@"U"]) {
            [arrayU addObject:dic];
        }if ([[dic objectForKey:@"title"] hasPrefix:@"V"]) {
            [arrayV addObject:dic];
//        }if ([[dic objectForKey:@"title"] hasPrefix:@"W"]) {
//            [arrayW addObject:dic];
//        }if ([[dic objectForKey:@"title"] hasPrefix:@"X"]) {
//            [arrayX addObject:dic];
//        }if ([[dic objectForKey:@"title"] hasPrefix:@"Y"]) {
//            [arrayY addObject:dic];
        }if ([[dic objectForKey:@"title"] hasPrefix:@"Z"]) {
            [arrayZ addObject:dic];
        }
        
    }
    
    [self.arrayAll addObject:arrayA];
    [self.arrayAll addObject:arrayB];
    [self.arrayAll addObject:arrayC];
    [self.arrayAll addObject:arrayD];
    [self.arrayAll addObject:arrayE];
    [self.arrayAll addObject:arrayF];
    [self.arrayAll addObject:arrayG];
    [self.arrayAll addObject:arrayH];
    [self.arrayAll addObject:arrayI];
    [self.arrayAll addObject:arrayJ];
//    [self.arrayAll addObject:arrayK];
    [self.arrayAll addObject:arrayL];
    [self.arrayAll addObject:arrayM];
    [self.arrayAll addObject:arrayN];
    [self.arrayAll addObject:arrayO];
    [self.arrayAll addObject:arrayP];
//    [self.arrayAll addObject:arrayQ];
    [self.arrayAll addObject:arrayR];
    [self.arrayAll addObject:arrayS];
    [self.arrayAll addObject:arrayT];
    [self.arrayAll addObject:arrayU];
    [self.arrayAll addObject:arrayV];
//    [self.arrayAll addObject:arrayW];
//    [self.arrayAll addObject:arrayX];
//    [self.arrayAll addObject:arrayY];
    [self.arrayAll addObject:arrayZ];
}



#pragma mark -
#pragma mark 创建collectionView

-(void)createCollection
{
    UICollectionViewFlowLayout *flow = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    flow.itemSize = CGSizeMake(self.view.frame.size.width / 2 - 30, self.view.frame.size.height * 0.1 );
    flow.headerReferenceSize = CGSizeMake(self.view.frame.size.width - 40, 40);
   
    _collection = [[[UICollectionView alloc] initWithFrame:CGRectMake(20, 120, self.view.frame.size.width - 40, self.view.frame.size.height - 130 - 44) collectionViewLayout:flow] autorelease];
//    [_collection setBackgroundColor:[UIColor colorWithRed:242 / 255.0F green:242 / 255.0F blue:242 / 255.0F alpha:1.0]];
    [_collection setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:_collection];
    _collection.delegate = self;
    _collection.dataSource = self;
    [_collection registerClass:[PinpaiCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [_collection registerClass:[HeadCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    
    _collection.alpha = 0.7;
}


#pragma mark -
#pragma mark collection的协议

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return [self.arrayAll count];
}


-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    HeadCollectionReusableView *hea = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
    [hea.head setTextColor: [UIColor blueColor]];
    switch (indexPath.section) {
        case 0:
            [hea.head setText:@"A"];
            break;
        case 1:
            [hea.head setText:@"B"];
            break;
        case 2:
            [hea.head setText:@"C"];
            break;
        case 3:
            [hea.head setText:@"D"];
            break;
        case 4:
            [hea.head setText:@"E"];
            break;
        case 5:
            [hea.head setText:@"F"];
            break;
        case 6:
            [hea.head setText:@"G"];
            break;
        case 7:
            [hea.head setText:@"H"];
            break;
        case 8:
            [hea.head setText:@"I"];
            break;
        case 9:
            [hea.head setText:@"J"];
            break;
        case 10:
            [hea.head setText:@"L"];
            break;
        case 11:
            [hea.head setText:@"M"];
            break;
        case 12:
            [hea.head setText:@"N"];
            break;
        case 13:
            [hea.head setText:@"O"];
            break;
        case 14:
            [hea.head setText:@"P"];
            break;
        case 15:
            [hea.head setText:@"R"];
            break;
        case 16:
            [hea.head setText:@"S"];
            break;
        case 17:
            [hea.head setText:@"T"];
            break;
        case 18:
            [hea.head setText:@"U"];
            break;
        case 19:
            [hea.head setText:@"V"];
            break;
        case 20:
            [hea.head setText:@"Z"];
            break;
        default:
            break;
    }
    return hea;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [[self.arrayAll objectAtIndex:section] count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PinpaiCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSMutableArray *arr= [self.arrayAll objectAtIndex:indexPath.section];
    NSDictionary *dic = [arr objectAtIndex:indexPath.item];
    [cell.image sd_setImageWithURL:[dic objectForKey:@"cover_thumb"]];

    cell.backgroundColor = [UIColor colorWithRed:255/255.0F green:255/255.0F                                                                                                                                                                                                                                                                                                                                                                                                                                                      blue:255/255.0F alpha:1.0] ;
    cell.layer.borderWidth = 1.0f;
    
    
    return cell;
}
// 取消选中状态
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    
    NSArray *array = [self.arrayAll objectAtIndex:indexPath.section];
    NSDictionary *dic = [array objectAtIndex:indexPath.item];
    NSString *name = [dic objectForKey:@"title"];
    PinViewController *pin = [[PinViewController alloc] init];
    pin.name = name;
    pin.dic = dic;
    [self.navigationController pushViewController:pin animated:YES];
    [pin release];
   
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
